/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.CutDetail;
import com.click.app.model.CutProcess;
import com.click.app.model.Material;
import com.click.app.model.Model;
import com.click.app.model.ModelSize;
import com.click.app.model.PantDesign;
import com.click.app.model.StorageCut;
import com.click.app.service.CutDetailManager;
import com.click.app.service.CutProcessManager;
import com.click.app.service.GenericManager;
import com.click.app.service.MaterialManager;
import com.click.app.service.PantDesignManager;
import com.click.app.service.StorageCutManager;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ariel
 */
public class CutDetailAction extends BaseAction {

    private CutDetailManager cutDetailManager;
    private CutProcessManager cutProcessManager;
    private GenericManager<Model, Long> modelManager;
    private GenericManager<ModelSize, Long> modelSizeManager;
    private StorageCutManager storageCutManager;
    private List<CutDetail> cutDetails;
    private CutDetail cutDetail;
    private Long id;
    private Integer quantity = 0;
    private CutProcess cutProcess;
    private Long idCutProcess;
    private PantDesignManager pantDesignManager;

    private List<Material>listMaterials;
    private MaterialManager materialManager;
    
    private Long idTemp;
    
    public void setId(Long id) {
        this.id = id;
    }

    public void setIdCutProcess(Long idCutProcess) {
        this.idCutProcess = idCutProcess;
    }

    public CutDetail getCutDetail() {
        return cutDetail;
    }

    public void setCutDetail(CutDetail cutDetail) {
        this.cutDetail = cutDetail;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public CutProcess getCutProcess() {
        return cutProcess;
    }

    public List<CutDetail> getCutDetails() {
        return cutDetails;
    }

    public String editCutDetail() {
        if (id != null) {
            cutDetail = cutDetailManager.get(id);
        } else {
            cutDetail = new CutDetail();
            cutDetail.setQuantity(1);
        }
        return SUCCESS;
    }

    public String saveCutDetail() throws Exception {
        System.out.println("saveCutDetail() ");
        Long idCutProcess = (Long)getSession().getAttribute("idCutProcess");
        cutProcess = cutProcessManager.get(idCutProcess);
        PantDesign pantDesign = null;

        if(cutDetail.getId() != null){
             
            if(!cutDetail.getPantDesign().getModel().getId().equals("-1") &&
                !cutDetail.getPantDesign().getModelSize().getId().equals("-1") &&
                !cutDetail.getPantDesign().getMaterial().getId().equals("-1")){
                    
                    CutDetail cutDetailAux = cutDetailManager.get(cutDetail.getId());

                    List<PantDesign> listPantDesign = (List<PantDesign>) pantDesignManager.findByModel( cutDetail.getPantDesign().getModel().getId()
                                                                                                      , cutDetail.getPantDesign().getModelSize().getId()
                                                                                                      , cutDetail.getPantDesign().getMaterial().getId());
                    if (listPantDesign != null && listPantDesign.size()>0) {
                        pantDesign = listPantDesign.get(0);
                        
                    } else {
                        
                        pantDesign = new PantDesign();
                        pantDesign.setModel(modelManager.get(cutDetail.getPantDesign().getModel().getId()));
                        pantDesign.setModelSize(modelSizeManager.get(cutDetail.getPantDesign().getModelSize().getId()));
                        pantDesign.setMaterial(materialManager.get(cutDetail.getPantDesign().getMaterial().getId()));
                        pantDesign = pantDesignManager.save(pantDesign);
                    }
                    cutDetailAux.setPantDesign(pantDesign);
                    cutDetailManager.save(cutDetailAux);
                    String key = "cutDetail.updated";
                    saveMessage(getText(key));
           }
        }else{
             if(!cutDetail.getPantDesign().getModel().getId().equals("-1") &&
                !cutDetail.getPantDesign().getModelSize().getId().equals("-1") &&
                !cutDetail.getPantDesign().getMaterial().getId().equals("-1")){
                
                CutDetail cutDetailNew = new CutDetail();
                List<PantDesign> listPantDesign =
                        pantDesignManager.findByModel(modelManager.get(cutDetail.getPantDesign().getModel().getId()).getId(),
                                                      modelSizeManager.get(cutDetail.getPantDesign().getModelSize().getId()).getId(),
                                                      materialManager.get(cutDetail.getPantDesign().getMaterial().getId()).getId());
                if (listPantDesign != null && listPantDesign.size()>0) {
                    pantDesign = listPantDesign.get(0);
                } else {
                    pantDesign = new PantDesign();
                    pantDesign.setModel(modelManager.get(cutDetail.getPantDesign().getModel().getId()));
                    pantDesign.setModelSize(modelSizeManager.get(cutDetail.getPantDesign().getModelSize().getId()));
                    pantDesign.setMaterial(materialManager.get(cutDetail.getPantDesign().getMaterial().getId()));
                    pantDesign = pantDesignManager.save(pantDesign);
                }

                cutDetailNew.setPantDesign(pantDesign);
                cutDetailNew.setQuantity(cutDetail.getQuantity());
                cutDetailNew.setCutProcess(cutProcess);
                cutDetailNew.setQuantity(1);
                cutDetailNew.setPriceXclotheFail(0.0f);
                cutDetailNew.setQuantity(1);
                cutDetailNew.setQuantityFail(0);
                cutDetailNew.setPriceXclothes(0.0f);
                cutDetailNew.setQuantityRealClothes(0);
                cutDetailNew.setQuantityRealClothesCurrent(0);
                cutDetailNew.setTotalAmount(0.0f);
                cutDetailNew.setTotalAmountClothes(0.0f);
                cutDetailNew.setTotalAmountFailClothes(0.0f);
                cutDetailNew.setQuantityGet(0);
                cutDetailNew.setCurrentAmount(0.0f);
                cutDetailNew.setModify(true);
                cutDetailManager.save(cutDetailNew);
                saveMessage(getText("cutDetailAction.saveCutDetail"));
             }else{
                errorMessage(getText("cutDetailAction.errorCutDetail"));
                return ERROR;
             }
             
        }
        return SUCCESS;
    }

    public String saveCutDetailReview(){
        System.out.println("saveCutDetailReview() ");
        CutDetail cutDetailAux = cutDetailManager.get(cutDetail.getId());

            List<PantDesign> pantDesignList = (List<PantDesign>) pantDesignManager.findByModel( cutDetailAux.getPantDesign().getModel().getId()
                                                                                              , cutDetailAux.getPantDesign().getModelSize().getId()
                                                                                              , cutDetailAux.getPantDesign().getMaterial().getId());
            StorageCut storageCut = null;
            Integer stock = 0;
            if(!pantDesignList.isEmpty()){
                PantDesign pantDesignX = pantDesignList.get(0);
                List<StorageCut> storageCuts = (List<StorageCut>) storageCutManager.findByPantDesign(pantDesignX.getId());
                if (!storageCuts.isEmpty()) {
                    storageCut = storageCuts.get(0);
                    stock = (storageCut.getStock()==null? 0 : storageCut.getStock());
                    stock = stock + ( cutDetail.getQuantityGet() - cutDetailAux.getQuantityGet())
                                  - ( cutDetail.getQuantityFail() - cutDetailAux.getQuantityFail());

                } else {
                    storageCut = new StorageCut();
                    storageCut.setPantDesign(pantDesignX);
                    stock =   ( cutDetail.getQuantityGet() - cutDetailAux.getQuantityGet())
                            - ( cutDetail.getQuantityFail() - cutDetailAux.getQuantityFail());
                }
            }

            cutDetailAux.setQuantityGet(cutDetail.getQuantityGet());
            cutDetailAux.setPriceXclothes(cutDetail.getPriceXclothes());
            cutDetailAux.setTotalAmountClothes(cutDetail.getQuantityGet() * cutDetail.getPriceXclothes());

            cutDetailAux.setQuantityFail(cutDetail.getQuantityFail());
            cutDetailAux.setPriceXclotheFail(cutDetail.getPriceXclotheFail());
            cutDetailAux.setTotalAmountFailClothes(cutDetail.getQuantityFail() * cutDetail.getPriceXclotheFail());

            cutDetailAux.setTotalAmount((cutDetail.getQuantityGet() * cutDetail.getPriceXclothes()) -
                                        (cutDetail.getQuantityFail() * cutDetail.getPriceXclotheFail()));
            cutDetailAux.setCurrentAmount((cutDetail.getQuantityGet() * cutDetail.getPriceXclothes()) -
                                        (cutDetail.getQuantityFail() * cutDetail.getPriceXclotheFail()));
            cutDetailAux.setQuantityRealClothes(cutDetail.getQuantityGet() - cutDetail.getQuantityFail());
            cutDetailAux.setQuantityRealClothesCurrent(cutDetail.getQuantityGet() - cutDetail.getQuantityFail());
            cutDetailAux.setModify(false);
            storageCut.setStock(stock);
            storageCutManager.save(storageCut);
            cutDetailManager.save(cutDetailAux);
            saveMessage(getText("cutDetailAction.upDateCutDetail"));
            System.out.println("end saveCutDetailReview() ");
            return SUCCESS;
    }
    public String cancelCutDetailReview(){
        
        return CANCEL;
    }
    public void setCutDetailManager(CutDetailManager cutDetailManager) {
        this.cutDetailManager = cutDetailManager;
    }

    public void setCutProcessManager(CutProcessManager cutProcessManager) {
        this.cutProcessManager = cutProcessManager;
    }

    public void setModelManager(GenericManager<Model, Long> modelManager) {
        this.modelManager = modelManager;
    }

    public void setModelSizeManager(GenericManager<ModelSize, Long> modelSizeManager) {
        this.modelSizeManager = modelSizeManager;
    }

    public void setStorageCutManager(StorageCutManager storageCutManager) {
        this.storageCutManager = storageCutManager;
    }

    public List<Model> getModelList() {
        return modelManager.getAll();
    }

    public List<ModelSize> getModelSizeList() {
        return modelSizeManager.getAll();
    }

    public PantDesignManager getPantDesignManager() {
        return pantDesignManager;
    }

    public void setPantDesignManager(PantDesignManager pantDesignManager) {
        this.pantDesignManager = pantDesignManager;
    }

    public Long getIdTemp() {
        return idTemp;
    }

    public void setIdTemp(Long idTemp) {
        this.idTemp = idTemp;
    }

    public List<Material> getListMaterials() {
         if (listMaterials == null) {
            List<String> names = new ArrayList<String>();
            names.add("tipoMaterial");
            List<String> values = new ArrayList<String>();
            values.add("jean");
            List<String> opNames = new ArrayList<String>();
            opNames.add("=");
            List<String> opValues = new ArrayList<String>();
            opValues.add("=");
            List<String> joinsFields = new ArrayList<String>();
            listMaterials = materialManager.findMaterialByDistintive(names, opNames, values, opValues, joinsFields);
        }
        return listMaterials;
    }

    public void setListMaterials(List<Material> listMaterials) {
        this.listMaterials = listMaterials;
    }

    public MaterialManager getMaterialManager() {
        return materialManager;
    }

    public void setMaterialManager(MaterialManager materialManager) {
        this.materialManager = materialManager;
    }

    public Long getId() {
        return id;
    }

    public String editCutDetailReview(){
        cutDetail = cutDetailManager.get(id);
        return SUCCESS;
    }
}
